#include "test_mem.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"
#include "string.h"
#include "stdlib.h"
/*
CM:MEM_OPERATION:HEAP                //查询内存状态 
CM:MEM_OPERATION:MALLOC:[内存大小]    //分配指定大小的内存
CM:MEM_OPERATION:CALLOC:[内存块个数]:[大小]        //申请内存
CM:MEM_OPERATION:RE_ALLOC:[重新分配内存的地址]：[大小]     //重新分配内存
CM:MEM_OPERATION:FREE:[地址]                             //释放内存
 
*/


void cm_test_mem_operation(unsigned char*cmd[],int len)
{
    if(0 == strncmp("HEAP",cmd[2],strlen("HEAP")))
    {
        cm_heap_stats_t stats = {0};
        int ret = cm_mem_get_heap_stats(&stats);
        if(0 == ret)
        {
            cm_demo_printf("cm_mem_get_heap_stats RET:%d\n",ret);
            cm_demo_printf("[mem]Total Size:%d,Free Size:%d,Allocated Size:%d,max_continuity_free:%d\n",stats.total_size,stats.free,stats.allocated,stats.max_continuity_free);
        }
        else
        {
            cm_demo_printf("[mem]Error:%d\n",ret);
        }
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,ret,cm_mem_get_heap_stats);
    }else if(0 == strncmp("MALLOC",cmd[2],strlen("MALLOC")))
    {
        int size = atoi(cmd[3]);
        char *ptr = cm_malloc(size);
        cm_demo_printf("mem malloc ptr:%p, ptr:%u\n",ptr,ptr);
        OCTEST_FUNC_EXPECT_NOT_NULL(ptr,cm_malloc);
    }
    else if(0 == strncmp("RE_ALLOC",cmd[2],strlen("RE_ALLOC")))
    {
        char *ptr = strtoul(cmd[3],(void**)0,16);
        size_t size = atoi(cmd[4]);
        char *ptr_r = cm_realloc(ptr,size);
        cm_demo_printf("cm_realloc ret:%p\n",ptr_r);
        OCTEST_FUNC_EXPECT_NOT_NULL(ptr_r,cm_realloc);
    }
    else if(0 == strncmp("CALLOC",cmd[2],strlen("CALLOC")))
    {   
        size_t nitems = atoi(cmd[3]);
        size_t size =atoi(cmd[4]);
        char *ptr = cm_calloc(nitems,size);
        cm_demo_printf("cm_calloc ret:%p\n",ptr);
        OCTEST_FUNC_EXPECT_NOT_NULL(ptr,cm_calloc);
    }
    else if(0 == strncmp("FREE",cmd[2],strlen("FREE")))
    {
        char *ptr1;
        uint32_t *ptr = strtoul(cmd[3],&ptr1,16);
        cm_demo_printf("ptr:%p, ptr:%u\n",ptr,ptr);
        cm_free(ptr);
    }
    else if(0 == strncmp("LLOC_FREE",cmd[2],strlen("LLOC_FREE")))
    {
        int size = 200000;
        char *ptr = cm_malloc(size);
        cm_demo_printf("mem malloc ptr:%p, ptr:%u\n",ptr,ptr);
        OCTEST_FUNC_EXPECT_NOT_NULL(ptr,cm_malloc);        
        osDelay(10);
        cm_free(ptr);
        ptr = cm_malloc(size);
        cm_demo_printf("mem malloc ptr:%p, ptr:%u\n",ptr,ptr);
        size_t re_size = 20000;
        osDelay(10);
        char *ptr_r = cm_realloc(ptr,re_size);
        cm_demo_printf("cm_realloc ret:%p\n",ptr_r);
        OCTEST_FUNC_EXPECT_NOT_NULL(ptr_r,cm_realloc);
        cm_free(ptr_r);
        osDelay(10);
        size_t nitems = 100;
        size_t nsize =1024;
        char *ptr_c = cm_calloc(nitems,nsize);
        cm_demo_printf("cm_calloc ret:%p\n",ptr_c);
        OCTEST_FUNC_EXPECT_NOT_NULL(ptr_c,cm_calloc);
        cm_free(ptr_c);
        osDelay(10);
        cm_heap_stats_t stats = {0};
        int ret = cm_mem_get_heap_stats(&stats);
        if(0 == ret)
        {
            cm_demo_printf("cm_mem_get_heap_stats RET:%d\n",ret);
            cm_demo_printf("[mem]Total Size:%d,Free Size:%d,Allocated Size:%d,max_continuity_free:%d\n",stats.total_size,stats.free,stats.allocated,stats.max_continuity_free);
        }
        else
        {
            cm_demo_printf("[mem]Error:%d\n",ret);
        }
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,ret,cm_mem_get_heap_stats);

    }
    else{
        cm_demo_printf("param error!\n");
    }
}